﻿using LiveCharts.Wpf;
using Spire.Additions.Xps.Schema;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using UserControlLib;

namespace Wpf_Examples.Views
{
    /// <summary>
    /// ArcProgressBarWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ArcProgressBarWindow : Window
    {
        public ArcProgressBarWindow()
        {
            InitializeComponent();


            //创建定时器
            CreateTimer();
        }

        private int maxValue = 100;
        private int minValue = 0;

        /// <summary>
        /// 计算弧线终点坐标
        /// </summary>
        /// <param name="Value">弧形当前值</param>
        /// <param name="Min">弧形最小值</param>
        /// <param name="Max">弧形最大值</param>
        /// <param name="totalAngle">整个弧形的角度</param>
        /// <param name="offsetAngle">弧形缺失的角度</param>
        private void CalculateEndPoint(int Value,int Min,int Max,int totalAngle,int offsetAngle)
        {
            //值有效性校验
            int value = Value >= Max ? Max : Value <= Min ? Min : Value;
            int radius = 90;//圆弧半径

            //计算圆弧坐标 
            // 1、计算圆弧角度  我们给弧度设定一个最小值和最大值，最小值表示0度，对最大值表示圆弧最大角度 这里我们的圆弧最大角度为270度
            //根据相似性就有以下等式成立  (当前圆弧值-最小值)/(最大值-最小值)=当前圆弧角度/总的圆弧角度
            //得到 当前圆弧角度=总的圆弧角度*(当前圆弧值-最小值)/(最大值-最小值)

            //2、计算角度
            double angle = totalAngle / (Max - Min) * (Value - Min);
            //3、计算弧度
            //计算弧度 加上圆形缺失的角度值(面板有90度是缺失的)
            double rad = (Math.PI / 180) * (angle + offsetAngle);

            //4、计算弧线终点坐标
            //x = 圆心x + 半径 × cos(角度)。
            double x = radius * Math.Cos(rad) + radius;
            //y = 圆心y + 半径 × sin(角度)。
            double y = radius * Math.Sin(rad) + radius;
            Point point = new Point(x, y);
            pathArc.IsLargeArc = angle > 180;
            pathArc.Point = point;
        }

        private void CreateTimer()
        {
            #region 每秒定时器服务
            DispatcherTimer cpuTimer = new DispatcherTimer
            {
                Interval = new TimeSpan(0, 0, 0, 1, 0)
            };
            cpuTimer.Tick += DispatcherTimer_Tick;
            cpuTimer.Start();
            #endregion
        }

        private void DispatcherTimer_Tick(object sender, EventArgs e)
        {
            Random rnd = new Random();
            int val = rnd.Next(minValue, maxValue);
            CalculateEndPoint(val, minValue, maxValue,270,90);
        }
    }

}
